package com.easy.dn.ui.mainFragment;

import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.easy.common.base.BaseFragment;
import com.easy.common.baserx.RxSubscriber;
import com.easy.common.commonutils.ImageLoaderUtils;
import com.easy.common.commonutils.StringUtils;
import com.easy.common.imagePager.BigImagePagerActivity;
import com.easy.dn.api.Api;
import com.easy.dn.api.ApiConstants;
import com.easy.dn.api.ApiService;
import com.easy.dn.app.AppConstant;
import com.easy.dn.bean.InfoRefreshBean;
import com.easy.dn.bean.UserDataBean;
import com.easy.dn.ui.usercenter.activity.LoginActivity;
import com.easy.dn.ui.usercenter.activity.MyInfomationActivity;
import com.easy.dn.ui.usercenter.activity.RemarkActivity;
import com.easy.dn.ui.usercenter.activity.SplashActivity;
import com.easy.dn.utils.MapHelper;
import com.makeramen.roundedimageview.RoundedImageView;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;
import dn.easy.com.easyto.R;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by 丁宁
 * on 2017/5/2.
 * 个人中心
 */

public class UserCenterFragment extends BaseFragment {

    @Bind(R.id.admin)
    TextView mAdmin;
    @Bind(R.id.personal_vip_level)
    ImageView mPersonalVipLevel;
    @Bind(R.id.tv_position)
    TextView mTvPosition;
    @Bind(R.id.tv_company)
    TextView mTvCompany;
    @Bind(R.id.usercenter_headImg)
    RoundedImageView mUsercenterHeadImg;
    @Bind(R.id.tv_deal_count)
    TextView mTvDealCount;
    @Bind(R.id.usercenter_relevant)
    TextView mUsercenterRelevant;
    @Bind(R.id.usercenter_reflect)
    TextView mUsercenterReflect;
    @Bind(R.id.usercenter_see_resume)
    TextView mUsercenterSeeResume;
    @Bind(R.id.tv_resume_screen_count)
    TextView mTvResumeScreenCount;
    @Bind(R.id.relative_usercenter_download_resume)
    RelativeLayout mRelativeUsercenterDownloadResume;
    @Bind(R.id.relative_usercenter_remark)
    RelativeLayout mRelativeUsercenterRemark;
    @Bind(R.id.relative_usercenter_vip)
    RelativeLayout mRelativeUsercenterVip;
    @Bind(R.id.relative_usercenter_info)
    RelativeLayout mRelativeUsercenterInfo;
    @Bind(R.id.relative_usercenter_feed_back)
    RelativeLayout mRelativeUsercenterFeedBack;
    @Bind(R.id.relative_usercenter_setting)
    RelativeLayout mRelativeUsercenterSetting;
    private List<View> mGuideViewList;
    private List<String> mImgUrls;

    @Override
    protected int getLayoutResource() {
        return R.layout.fra_user_center;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    protected void initView() {
        mImgUrls = new ArrayList<>();
        //头像点击放大
        mUsercenterHeadImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BigImagePagerActivity.startImagePagerActivity(getActivity(), mImgUrls, 0);
            }
        });
        loadData();
        // 登錄成功之後刷新界面
        mRxManager.on(AppConstant.USERCENTERREFRESH, new Action1<String>() {
            @Override
            public void call(String isRefresh) {
                if (isRefresh.equals("ISFRESH"))
                    loadData();
            }
        });
        // 更改我的信息之後刷新界面
        mRxManager.on(AppConstant.INFOREFRESH, new Action1<InfoRefreshBean>() {
            @Override
            public void call(InfoRefreshBean bean) {
                loadData();
//                if (bean.getFlag()==1){
//
//                }else if (bean.getFlag()==2){
//
//                }else if (bean.getFlag()==3){
//
//                }
            }
        });
    }


    @OnClick({R.id.admin, R.id.relative_usercenter_download_resume, R.id.relative_usercenter_remark, R.id.relative_usercenter_vip, R.id.relative_usercenter_info, R.id.relative_usercenter_feed_back, R.id.relative_usercenter_setting})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.admin:
                startActivity(LoginActivity.class);
                break;
            case R.id.relative_usercenter_download_resume:
                //进入我的备注
                startActivity(SplashActivity.class);

                break;
            case R.id.relative_usercenter_remark:
                //进入我的备注
                startActivity(RemarkActivity.class);
                break;
            case R.id.relative_usercenter_vip:
                break;
            case R.id.relative_usercenter_info:
                //进入我的信息
                startActivity(MyInfomationActivity.class);

                break;
            case R.id.relative_usercenter_feed_back:
                break;
            case R.id.relative_usercenter_setting:
                break;
        }
    }

    //请求数据
    private void loadData() {
        Map<String, String> map = MapHelper.getAccessMap();
        Api.getInstance().getProxy(ApiService.class).getUserData(Api.getCacheControl(), map)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<UserDataBean>(getActivity(), false) {
                    @Override
                    protected void _onNext(UserDataBean userDataBean) {
                        if (userDataBean != null) {
                            if (isAdded()) {
                                mUsercenterRelevant.setText("" + decimal(userDataBean.getProfessionalRateAvg()));
                                mUsercenterReflect.setText("" + decimal(userDataBean.getResponseRateAvg()));
                                //头像
                                if (userDataBean.getHeadImgPath() != null) {
                                    mImgUrls.clear();
                                    ImageLoaderUtils.displaySmallPhoto(getActivity(), mUsercenterHeadImg, ApiConstants.URL + userDataBean.getHeadImgPath());
                                    mImgUrls.add(ApiConstants.URL + userDataBean.getHeadImgPath());
                                }
                                // vip
                                if (userDataBean.getVipLevel() == 10) {
                                    mPersonalVipLevel.setImageResource(R.drawable.vip_small);
                                } else if (userDataBean.getVipLevel() == 20) {
                                    mPersonalVipLevel.setImageResource(R.drawable.svip_small);
                                } else {
                                    mPersonalVipLevel.setVisibility(View.GONE);
                                }
                                //职位
                                if (!StringUtils.isEmpty(userDataBean.getPosition())) {
                                    mTvPosition.setVisibility(View.VISIBLE);
                                    mTvPosition.setText(userDataBean.getPosition());
                                } else {
                                    mTvPosition.setVisibility(View.GONE);
                                }

                                if (!StringUtils.isEmpty(userDataBean.getCompanyName())) {
                                    mTvCompany.setVisibility(View.VISIBLE);
                                    mTvCompany.setText(userDataBean.getCompanyName());
                                } else {
                                    mTvCompany.setVisibility(View.GONE);
                                }

                                if (StringUtils.isEmpty(userDataBean.getTurnoverCount())) {
                                    mTvDealCount.setText("0/0");
                                } else {
                                    mTvDealCount.setText(userDataBean.getTurnoverCount());
                                }
                                if (userDataBean.getViewPositionRate() == null || "".equals(userDataBean.getViewPositionRate())) {
                                    mUsercenterSeeResume.setText("0 %");
                                } else {
                                    mUsercenterSeeResume.setText(userDataBean.getViewPositionRate() + "%");
                                }
                                if (userDataBean.getPositionFilterRate() == null || "".equals(userDataBean.getPositionFilterRate())) {
                                    mTvResumeScreenCount.setText("0 %");
                                } else {
                                    mTvResumeScreenCount.setText(userDataBean.getPositionFilterRate() + "%");
                                }
                                mAdmin.setText(userDataBean.getNickname());
                            }
                        } else {
                            mAdmin.setText(R.string.person_admin);
                        }
                    }

                    @Override
                    protected void _onError(String message) {

                    }
                });
    }

    private double decimal(int model) {
        float c = model;
        double f = c / 10;
        BigDecimal b = new BigDecimal(f);
        //保留2位小数
        double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return f1;
    }
}
